package com.doge.walknovel.business.home.discover

import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.doge.walknovel.core.base.LoadStatus
import com.doge.walknovel.core.base.adapters.toLiveData

class DiscoverViewModel constructor(private val repository: DiscoverRepository) : ViewModel() {
    val loading: LiveData<LoadStatus> = repository.loading
    val dataList = repository.dataSource().toLiveData(
        cellId = { it.id },
        requestMore = { }) // no page

    fun request() {
        repository.request(justCache = false)
        repository.requestAd()
    }
}


class DiscoverViewModelFactory : ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        val repository = DiscoverRepository.get()
        if (modelClass.isAssignableFrom(DiscoverViewModel::class.java)) {
            return DiscoverViewModel(repository) as T
        }
        //noinspection unchecked
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}

